//Shamelessly copied(and modified) from the http://jqueryui.com/autocomplete/#multiple-remote example

/**
 * Adds multicomplete dropdown functionality to text elements.
 */
$.fn.multi_complete=function(args){
	var regex=new RegExp(args.pattern);
	$(this).bind("keydown",function(event){
			if(event.keyCode===$.ui.keyCode.TAB && $(this).data("ui-autocomplete").menu.active){
				event.preventDefault();
			}
	});
	$(this).autocomplete({
			source:function(request,response){
				var match=regex.exec(request.term);
				if(match){
					args.source({term:match[0]},response);
				}else{
					response();
				}
			},
			focus:function(){
				//prevent value inserted on focus
				return false;
			},
			select:function(event,ui){
				var newString=ui.item.value;
				if(args.padright){
					newString+=args.padright;
				}
				$(this).val($(this).val().replace(regex,newString));
				return false;
			}
	});
}
